
import com.desidea.webservice.PartnerService;
import com.desidea.webservice.ProcessServiceRequestResultEvent;

import flash.display.DisplayObject;
import flash.events.Event;

import mx.controls.Alert;
import mx.controls.Button;
import mx.controls.ComboBox;
import mx.controls.TextInput;
import mx.events.ListEvent;
import mx.events.ValidationResultEvent;
import mx.rpc.events.FaultEvent;

private var _cmbCities:ComboBox;
private var _txtCityName:TextInput;

private var _cityNo:Number = 0;
private var _userId:String = "0";
private var _isDataLoaded:Boolean = false;
private var _dataXml:XML;

private static var NEW:String = "New";
private static var CANCEL:String = "Cancel";


/*********************************************************************
 * EVENT HANDING
 *********************************************************************/
private function controlCreated():void{
	_cmbCities = new ComboBox();
	_cmbCities.percentWidth = 100;
	_cmbCities.addEventListener(ListEvent.CHANGE,citySelectionChange);
	
	_txtCityName = new TextInput();
	_txtCityName.percentWidth = 100;
	
	addControl(_cmbCities, NEW);
}
private function citySelectionChange(event:ListEvent):void{
	_cityNo = new Number(this._cmbCities.selectedItem.@id);
}
private function onBtnClick():void{
	if(this.btnNewCancel.label == NEW)
		onNew();
	else if(this.btnNewCancel.label == CANCEL)
		onCancel();	
}
private function onNew():void{
	addControl(this._txtCityName,CANCEL);
	this._txtCityName.setFocus();
}
private function onCancel():void{
	addControl(this._cmbCities,NEW);	
}

[Event(name="dataLoadComplete",type="flash.events.Event")]

private function callForCityList(event:ProcessServiceRequestResultEvent):void{

	_dataXml = XML(event.result);
	this._cmbCities.dataProvider = _dataXml.entry.(@active=="1" || @id == this._cityNo.toString()) as XMLList;
	this._cmbCities.selectedItem = (_dataXml.entry.(@id==this._cityNo) as XMLList)[0] as XML;
	
	_isDataLoaded = true;
	
	this.dispatchEvent( new Event("dataLoadComplete"));
}
private function partnerServiceFaultHandler(event:FaultEvent):void{
	Alert.show(event.fault.message,"Error: Cities");	
}

/***************************************************************
 * PRIVATE METHODS
 * *************************************************************/
private function addControl(ctl:DisplayObject, btnLabel:String):void{
	if(this.getChildAt(0) is Button){
		this.addChildAt(ctl,0)
	}else{
		this.removeChildAt(0);
		this.addChildAt(ctl,0)
	}
	this.btnNewCancel.label = btnLabel;
}

/***************************************************************
 * PUBLIC METHODS
 * *************************************************************/
public function loadData(userID:String, selectedCityNo:Number=0):void{
	var params:String = "OPERATION=CITIES_XMLLIST" +
						"&USERID=" + userID;
	
	var ws:PartnerService = new PartnerService();
	ws.addprocessServiceRequestEventListener(callForCityList);
	ws.addPartnerServiceFaultEventListener(partnerServiceFaultHandler);
	ws.processServiceRequest(params);
	
	this._cityNo = selectedCityNo;
}

public function get SelectedCityNo():Number{
	return (this.btnNewCancel.label==CANCEL ? -1: _cityNo);
}
public function set SelectedCityNo(value:Number):void{
	this._cityNo = value;
	if(_isDataLoaded){
		this._cmbCities.dataProvider = _dataXml.entry.(@active=="1" || @id == this._cityNo.toString()) as XMLList;
		this._cmbCities.selectedItem = (this._dataXml.entry.(@id==value) as XMLList)[0] as XML;
	}	
}
public function get CityName():String{
	return (this.btnNewCancel.label==NEW ? this._cmbCities.text:this._txtCityName.text );
}

public function setMode(isEdit:Boolean):void{
	this.btnNewCancel.enabled = isEdit;	
}

public function validate(required:Boolean): ValidationResultEvent{
	if(this.btnNewCancel.label == NEW){
		cityIdV.source = this._cmbCities;
		cityIdV.listener = this._cmbCities;
		cityIdV.required = required;
		return cityIdV.validate();
	}else{
		cityNameV.source = this._txtCityName;
		cityNameV.listener = this._txtCityName;
		cityNameV.required = required;
		return cityNameV.validate();
	}
}